<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Stock_model
 *
 * @author admin
 */
class stock_model extends CI_Model {

    private $_table_stock = 'vpc_stock';
    private $_table_stock_image = 'vpc_stock_image';
    private $_table_catalog_autopart = 'vpc_catalog_autopart';

    public function save_data($stock_name, $main_type_id, $brand_id, $stock_price, $stock_qty, $stock_classifire, $alert_below, $stock_code = NULL, $sub_type_id = NULL, $stock_detail = NULL) {
        $arr_stock = array('stock_code' => $stock_code,
            'stock_name' => $stock_name,
            'main_type_id' => $main_type_id,
            'sub_type_id' => $sub_type_id,
            'brand_id' => $brand_id,
            'stock_price' => $stock_price,
            'stock_qty' => $stock_qty,
            'classifire' => $stock_classifire,
            'stock_detail' => $stock_detail,
            'alert_below' => $alert_below,
            'is_deleted' => 'N',
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $can_save = $this->db->insert($this->_table_stock, $arr_stock);
        return $can_save;
    }

    public function update_data($id, $stock_name, $main_type_id, $brand_id, $stock_price, $stock_qty, $stock_classifire, $alert_below, $stock_code = NULL, $sub_type_id = NULL, $stock_detail = NULL) {
        $arr_stock = array('stock_code' => $stock_code,
            'stock_name' => $stock_name,
            'main_type_id' => $main_type_id,
            'sub_type_id' => $sub_type_id,
            'brand_id' => $brand_id,
            'stock_price' => $stock_price,
            'stock_qty' => $stock_qty,
            'classifire' => $stock_classifire,
            'stock_detail' => $stock_detail,
            'alert_below' => $alert_below,
            'is_deleted' => 'N',
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('id' => $id, 'is_deleted' => 'N');

        $can_save = $this->db->update($this->_table_stock, $arr_stock, $arr_where);
        return $can_save;
    }

    public function update_stock($id, $stock_qty) {
        $arr_stock = array('stock_qty' => $stock_qty,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('id' => $id, 'is_deleted' => 'N');

        $can_save = $this->db->update($this->_table_stock, $arr_stock, $arr_where);
        return $can_save;
    }

    public function get_stock_by_name($main_type_id, $sub_type_id, $brand_id, $stock_name) {
        $this->db->where('is_deleted', 'N');
        $query = $this->db->get_where($this->_table_stock, array('main_type_id' => $main_type_id,
            'sub_type_id' => $sub_type_id,
            'brand_id' => $brand_id,
            'stock_name' => $stock_name));
        return $query->result();
    }

    public function get_stock_by_brand_id($brand_id) {
        $this->db->where('is_deleted', 'N');
        $query = $this->db->get_where($this->_table_stock, array('brand_id' => $brand_id));
        return $query->result();
    }

    public function get_stock_by_id($id) {
        $this->db->select('*');
        $this->db->select($this->_table_stock . '.id as stock_id');
        $this->db->select('a.name as main_type_name');
        $this->db->select('b.name as sub_type_name');
        $this->db->select('c.name as brand_name');
        $this->db->where($this->_table_stock . '.is_deleted', 'N');
        $this->db->join($this->_table_catalog_autopart . ' as a', 'a.id = ' . $this->_table_stock . '.main_type_id', 'left');
        $this->db->join($this->_table_catalog_autopart . ' as b', 'b.id = ' . $this->_table_stock . '.sub_type_id', 'left');
        $this->db->join($this->_table_catalog_autopart . ' as c', 'c.id = ' . $this->_table_stock . '.brand_id', 'left');
        $query = $this->db->get_where($this->_table_stock, array($this->_table_stock . '.id' => $id));
        return $query->result();
    }

    public function save_image($id, $name) {
        $arr_stock_image = array('stock_id' => $id,
            'file_name' => $name,
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $this->db->insert($this->_table_stock_image, $arr_stock_image);
    }

    public function update_image($id, $name) {
        $arr_stock_image = array('file_name' => $name,
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );
        $arr_where = array('stock_id', $id);
        $this->db->update($this->_table_stock_image, $arr_stock_image, $arr_where);
    }

    public function count($code = NULL, $name = NULL, $main_id = NULL, $sub_id = NULL, $brand_id = NULL) {
        if (!is_null($code) && !empty($code)) {
            $this->db->like('stock_code', $code);
        }
        if (!is_null($name) && !empty($name)) {
            $this->db->like('stock_name', $name);
        }
        if (!is_null($main_id) && !empty($main_id)) {
            $this->db->where('main_type_id', $main_id);
        }
        if (!is_null($sub_id) && !empty($sub_id)) {
            $this->db->where('sub_type_id', $sub_id);
        }
        if (!is_null($brand_id) && !empty($brand_id)) {
            $this->db->where('brand_id', $brand_id);
        }
        $this->db->where('is_deleted', 'N');

        $query = $this->db->get_where($this->_table_stock);
        return count($query->result());
    }

    public function get_stock_img_by_id($id) {
        $query = $this->db->get_where($this->_table_stock_image, array('stock_id' => $id));
        return $query->result();
    }

    public function search_list_stock($code = NULL, $name = NULL, $main_id = NULL, $sub_id = NULL, $brand_id = NULL, $limit = 10, $offset = 0) {
        $this->db->select('*');
        $this->db->select($this->_table_stock . '.id as stock_id');
        $this->db->select('a.name as main_type_name');
        $this->db->select('b.name as sub_type_name');
        $this->db->select('c.name as brand_name');

        if (!is_null($code) && !empty($code)) {
            $this->db->like('stock_code', $code);
        }
        if (!is_null($name) && !empty($name)) {
            $this->db->like('stock_name', $name);
        }
        if (!is_null($main_id) && !empty($main_id)) {
            $this->db->where('main_type_id', $main_id);
        }
        if (!is_null($sub_id) && !empty($sub_id)) {
            $this->db->where('sub_type_id', $sub_id);
        }
        if (!is_null($brand_id) && !empty($brand_id)) {
            $this->db->where('brand_id', $brand_id);
        }

        $this->db->where($this->_table_stock . '.is_deleted', 'N');

        $this->db->join($this->_table_catalog_autopart . ' as a', 'a.id = ' . $this->_table_stock . '.main_type_id', 'left');
        $this->db->join($this->_table_catalog_autopart . ' as b', 'b.id = ' . $this->_table_stock . '.sub_type_id', 'left');
        $this->db->join($this->_table_catalog_autopart . ' as c', 'c.id = ' . $this->_table_stock . '.brand_id', 'left');
        $query = $this->db->get($this->_table_stock, $limit, $offset);
        return $query->result();
    }

    public function delete_stock($id) {
        $arr_driver = array('is_deleted' => 'Y',
            'updated_by' => $this->session->userdata('username'),
            'updated_date' => date("Y-m-d H:i:s")
        );

        $arr_where = array('id' => $id);
        $this->db->update($this->_table_stock, $arr_driver, $arr_where);
    }

    public function list_stock($main_id, $sub_id, $brand) {
        $query = $this->db->get_where($this->_table_stock, array('main_type_id' => $main_id, 'sub_type_id' => $sub_id, 'brand_id' => $brand));
        $arr_brand = array();
        foreach ($query->result() as $list) {
            if ($list->is_deleted != "Y") {
                $arr_brand[$list->id] = $list->stock_name;
            }
        }
        return $arr_brand;
    }

}
